package ${basePackageName}.domain.mybatis.entity;

<#if common>
import com.zy.explore.common.infrastructure.base.mybatis.TkBaseEntity;
import com.zy.explore.common.interfaces.frm.SortFieldFrm;
    <#if pkType == 'String'>
import com.zy.explore.common.infrastructure.util.MyUUIDGenerator;
import tk.mybatis.mapper.annotation.KeySql;
    <#else>
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
    </#if>
<#else>
import ${basePackageName}.infrastructure.base.mybatis.TkBaseEntity;
import ${basePackageName}.interfaces.frm.SortFieldFrm;
    <#if pkType == 'String'>
import ${basePackageName}.infrastructure.util.MyUUIDGenerator;
import tk.mybatis.mapper.annotation.KeySql;
    <#else>
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
    </#if>
</#if>
import ${basePackageName}.interfaces.frm.${tableClassNamePrefix}QueryFrm;
import javax.persistence.Column;
import javax.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
<#if pkType == 'String'>
import tk.mybatis.mapper.annotation.KeySql;
</#if>
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;

/**
 * 表(${tableName})对应tk.mybatis的ORM实体对象
 *
 * @author ${authorName}
 * @date ${dateTime}
 */
@Slf4j
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "${tableName}")
public class <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix} extends TkBaseEntity<<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}, ${pkType}> {
<#list tableColumns as column>
    <#if column.isPk>
    /**
     * 主键
     */
    @Id
        <#if pkType == 'String'>
    @KeySql(genId = MyUUIDGenerator.class)
        <#else>
    @Column(insertable = false, name = "${column.columnName}")
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
        </#if>
    private ${column.propType} ${column.propName};
    <#else>
    /**
     * ${(column.columnComment != '')? string('${column.columnComment}','TODO 补充字段注释')}
     */
    @Column(name = "${column.columnName}")
    private ${column.propType} ${column.propName};
    </#if>

</#list>
    /**
     * Frm 转 Example
     *
     * @param frm 参数对象
     * @return 返回Example对象
     */
    public static Example getExample(${tableClassNamePrefix}QueryFrm frm) {
        // TODO 请按实际需求组装查询条件
        log.warn("TODO 请按实际需求组装查询条件");
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        Example example = new Example(<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.class);
        Example.Criteria criteria = example.createCriteria();
    <#list tableColumns as column>
        <#if column.propType == 'Integer' || column.propType == 'Long' || column.propType == 'Float' || column.propType == 'Double' || column.propType == 'Boolean'>
        if(frm.get${column.firstUpperPropName}() != null) {
            criteria.andEqualTo("${column.propName}", frm.get${column.firstUpperPropName}());
        }
        <#elseif column.propType == 'Date'>
        if(!StringUtils.isEmpty(frm.get${column.firstUpperPropName}Begin())) {
            criteria.andGreaterThanOrEqualTo("${column.propName}", frm.get${column.firstUpperPropName}Begin());
        }
        if(!StringUtils.isEmpty(frm.get${column.firstUpperPropName}End())) {
            criteria.andLessThanOrEqualTo("${column.propName}", frm.get${column.firstUpperPropName}End());
        }
        <#elseif column.propType == 'String'>
        if (!StringUtils.isEmpty(frm.get${column.firstUpperPropName}())) {
            criteria.andLike("${column.propName}", "%" + frm.get${column.firstUpperPropName}() + "%");
        }
        </#if>
    </#list>
    // 排序
        List<SortFieldFrm> sortFields = frm.getSortFields();
        if (!CollectionUtils.isEmpty(sortFields)) {
            sortFields.forEach(sortField -> {
                if (Sort.Direction.ASC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).asc();
                } else if (Sort.Direction.DESC.name().equalsIgnoreCase(sortField.getOrder())) {
                    example.orderBy(sortField.getPropName()).desc();
                }
            });
        } else {
            // 默认按主键降序排序
            example.orderBy("${pkPropName}").desc();
        }
        return example;
    }
}